<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="../../../admin/css/reset.css" />
    <link rel="stylesheet" href="../../../component/pear/css/pear.css"/>
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="layui-form-item">
                <label for="" class="layui-form-label">角色名称</label>
                <div class="layui-input-block">
                    <input type="text" id="role_name" maxlength="16" name="role_name" lay-verify="required"
                           placeholder="角色名称" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="" class="layui-form-label">角色描述</label>
                <div class="layui-input-block">
                    <input type="text" id="description" maxlength="16" name="description"
                           placeholder="角色描述" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="" class="layui-form-label">权限</label>
                <div class="layui-input-block">
                    <div id="permission-tree" class="demo-tree"></div>
                </div>
            </div>
        </div>
    </div>
    <input id="id" type="hidden" value="{$role->id}"/>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>


<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script src="../../../lms/base64.js"></script>
<script src="../../../lms/common.js"></script>
<script src="../../../lms/config.js"></script>


<script>

    const PRIMARY_KEY = "id";

    const SELECT_API = lms_config.api_url + "/api/setting/role/select";
    const DETAIL_API = lms_config.api_url + "/api/setting/role/get";
    const INSERT_API = lms_config.api_url + "/api/setting/role/insert";
    const UPDATE_API = lms_config.api_url + "/api/setting/role/update";


    if (!checkLogin()) {
        location.href = lms_config.base_path + "/login.html";
    }


    layui.use(['form', 'tree', 'jquery', 'http'], function () {
        let form = layui.form;
        let tree = layui.tree;
        let $ = layui.jquery;
        const http = layui.http;


        tree.render({
            elem: '#permission-tree'
            , data: fMakeMenu(getUserInfo().rules, 2)
            , showCheckbox: true  //是否显示复选框
            , id: 'permissionTree'
            //,isJump: true //是否允许点击节点时弹出新窗口跳转
            , accordion: true
            , click: function (obj) {
                let data = obj.data;  //获取当前点击的节点数据
                layer.msg('状态：' + obj.state + '<br>节点数据：' + JSON.stringify(data));
            },
        });


        let method = 'POST';
        if (getQueryString('operation') === 'edit') {
            method = 'PUT';

            let ids = getQueryString('rules').split(',');

            //与本管理员的所有权限子节点 取交集
            ids = ids.filter(item => {
                return fGetLastNodes(getUserInfo().rules).includes(parseInt(item));
            });

            $('#role_name').attr("value", decodeURI(getQueryString('role_name')));
            $('#description').attr("value", decodeURI(getQueryString('description')));
            tree.setChecked('permissionTree', ids); //勾选指定节点
        }


        form.on('submit(save)', function (data) {

            //获取所有选中的节点id
            function getCheckedId(data) {
                var id = "";
                $.each(data, function (index, item) {
                    if (id != "") {
                        id = id + "," + item.id;
                    } else {
                        id = item.id;
                    }
                    //item 没有children属性
                    if (item.children != null) {
                        var i = getCheckedId(item.children);
                        if (i != "") {
                            id = id + "," + i;
                        }
                    }
                });
                return id;
            }

            data.field.rules = getCheckedId(tree.getChecked('permissionTree'));

            if (method === 'PUT') {
                data.field[PRIMARY_KEY] = getQueryString(PRIMARY_KEY)
            }
            $.ajax({
                url: method === 'POST' ? INSERT_API : UPDATE_API,
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(data.field),
                dataType: 'json',

                success: function (ret) {
                    if (ret && ret.code == 0) {
                        layer.msg(ret.msg, {
                            icon: 1,
                            time: 1000
                        }, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
                            parent.refresh();
                        });
                        return false;
                    } else {
                        layer.msg(ret.msg, {
                            icon: 2,
                            time: 1000
                        });
                        return false;
                    }
                },
                error: function (ret) {
                    alert("出错" + ret.status + "：" + ret.responseText);
                }, //表示如果请求响应出现错误，会执行的回调函数
            });

            return false;
        });
    })
</script>
</body>
</html>
